package com.jprocms.module.cms.dal.mysql.vote;

import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.mybatis.core.mapper.BaseMapperX;
import com.jpro.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.jprocms.module.cms.controller.admin.vote.vo.VoteListReqVO;
import com.jprocms.module.cms.controller.admin.vote.vo.VotePageReqVO;
import com.jprocms.module.cms.dal.dataobject.vote.VoteDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.lang.Nullable;

import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 投票 Mapper
 *
 * @author jprocms
 */
@Mapper
public interface VoteMapper extends BaseMapperX<VoteDO> {

    @Nullable
    VoteDO selectById(Long id);

    /**
     * 投票
     *
     * @param id 投票ID
     * @return 更新条数
     */
    int vote(@Param("id") Long id);

    IPage<VoteDO> selectPage(@Param("params") VotePageReqVO params, @Param("orders") Collection<OrderItem> orders, IPage<VoteDO> page);

    List<VoteDO> selectList(@Param("params") VoteListReqVO params, @Param("orders") Collection<OrderItem> orders);


    default PageResult<VoteDO> selectPage(VotePageReqVO reqVO) {
        LambdaQueryWrapperX<VoteDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<VoteDO>()
                .eqIfPresent(VoteDO::getSiteId, reqVO.getSiteId())
                .likeIfPresent(VoteDO::getTitle, reqVO.getTitle())
                .betweenIfPresent(VoteDO::getBeginDate, reqVO.getBeginDate())
                .eqIfPresent(VoteDO::getEnabled, reqVO.getEnabled());
        OffsetDateTime now = OffsetDateTime.now();
        Date nowDate = Date.from(now.toInstant());
        if (reqVO.getIsActive() != null && reqVO.getIsActive()) {
            lambdaQueryWrapperX.isNull(VoteDO::getBeginDate).or().le(VoteDO::getBeginDate, nowDate);
            lambdaQueryWrapperX.isNull(VoteDO::getEndDate).or().ge(VoteDO::getEndDate, nowDate);
        }
        return selectPage(reqVO, lambdaQueryWrapperX.orderByDesc(Arrays.asList(VoteDO::getVoteSort, VoteDO::getId)));
    }

    default List<VoteDO> selectList(VoteListReqVO reqVO) {
        LambdaQueryWrapperX<VoteDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<VoteDO>()
                .eqIfPresent(VoteDO::getSiteId, reqVO.getSiteId())
                .likeIfPresent(VoteDO::getTitle, reqVO.getTitle())
                .betweenIfPresent(VoteDO::getBeginDate, reqVO.getBeginDate())
                .eqIfPresent(VoteDO::getEnabled, reqVO.getEnabled());
        OffsetDateTime now = OffsetDateTime.now();
        Date nowDate = Date.from(now.toInstant());
        if (reqVO.getIsActive() != null && reqVO.getIsActive()) {
            lambdaQueryWrapperX.isNull(VoteDO::getBeginDate).or().le(VoteDO::getBeginDate, nowDate);
            lambdaQueryWrapperX.isNull(VoteDO::getEndDate).or().ge(VoteDO::getEndDate, nowDate);
        }
        return selectList(lambdaQueryWrapperX.orderByDesc(Arrays.asList(VoteDO::getVoteSort, VoteDO::getId)));
    }

}
